package ru.study.jdbc.orm;

import java.lang.reflect.Field;
import java.util.*;
import java.sql.*;

import ru.study.jdbc.entity.AbstractEntity;
import ru.study.jdbc.ext.Couple;

public class FactoryByOrm {
	//карта ключевого поля
	private Map<String,Map> mapkey;
	//ключ - название таблицы, значение - карта Поле объека и Колонка таблицы.	
	private Map<String,Map> mapfields;	
	
	public FactoryByOrm(Map<String, Map> mapkey, Map<String, Map> mapfields) {		
		this.mapkey =  mapkey;		
		this.mapfields = mapfields;
	}	
	
	public <T extends AbstractEntity>  String getInsertQuery(T entity) {
		/*insert into st_student (student_id,firstName,surName,patronymic,dateOfBirth,sex,educationYear) 
		values (11,'Sergeev','Semen','Semenovich','1986-03-12','M',2005);*/
			
		String sql = "insert into  ";						
		
		for (Map.Entry<String,Map> entry : mapfields.entrySet()) {
			String table = entry.getKey();
			Map<String,String> condition = entry.getValue();
			sql = sql+table+" ( ";
			String  valuesql = "values ( "; 
			for (Map.Entry<String,String> ent : condition.entrySet()) {
				String field = ent.getKey();		
				String column = ent.getValue();						
				sql=sql+column+",";
				valuesql =valuesql+"?," ;
			}
			sql=sql.substring(0, sql.length()-1);
			valuesql=valuesql.substring(0, valuesql.length()-1);
			
			sql = sql+" ) "+ valuesql+" ) ";
		}			
		
		System.out.println(sql);
		
		return sql;
	}
	
	
	public String getSelectSqlQuery() {		
		String sql = "select ";
				
		for (Map.Entry<String,Map> entry : mapfields.entrySet()) {
			String table = entry.getKey();
			Map<String,String> condition = entry.getValue();			
			for (Map.Entry<String,String> ent : condition.entrySet()) {
				String field = ent.getKey();		
				String column = ent.getValue();						
				sql=sql+column+",";
			}			
			sql=sql.substring(0, sql.length()-1)+" from "+table;
		}		
		
		for (Map.Entry<String,Map> entry : mapkey.entrySet()) {
			String table = entry.getKey();
			Map<String,String> condition = entry.getValue();
			sql = sql+" where "+table+".";
			for (Map.Entry<String,String> ent : condition.entrySet()) {
				String column = ent.getValue();
				sql=sql+column+"=";
			}
		}		
		return sql;
	}

	public  <T extends AbstractEntity> String getUpdateSqlQuery(T entity) {
		//String sql = "update st_student set firstName = 'QWERTY' where student_id = ?";
		String sql = "update ";		
		
		for (Map.Entry<String,Map> entry : mapfields.entrySet()) {
			String table = entry.getKey();
			Map<String,String> condition = entry.getValue();
			sql = sql + table + " set ";
			
			for (Map.Entry<String,String> ent : condition.entrySet()) {
				String field = ent.getKey();				
				String column = ent.getValue();				
				sql=sql+column+"=?,";
			}
			sql=sql.substring(0, sql.length()-1);
		}		
		
		for (Map.Entry<String,Map> entry : mapkey.entrySet()) {
			String table = entry.getKey();
			Map<String,String> condition = entry.getValue();
			sql = sql+" where "+table+".";
			for (Map.Entry<String,String> ent : condition.entrySet()) {
				String column = ent.getValue();
				sql=sql+column+"=?";
			}			
			//System.out.println(sql);
		}				
		return sql;
	}
	
	public  <T extends AbstractEntity> String getDeleteSqlQuery(T entity) {
		String sql = "delete ";
		
		for (Map.Entry<String,Map> entry : mapkey.entrySet()) {
			String table = entry.getKey();
			sql= sql + "from "+ table; 
			Map<String,String> condition = entry.getValue();
			sql = sql+" where "+table+".";
			for (Map.Entry<String,String> ent : condition.entrySet()) {
				String column = ent.getValue();
				sql=sql+column+"=?";
			}			
			System.out.println(sql);
		}
		return sql;
	}
	

	
}
